/*Summary-----------------------------------------------------------------------

------------------------------------------------------------------------------*/



*-------------------------------------------------------------------------------
*-----------------Counterfactual revenues and uptake----------------------------
*-------------------------------------------------------------------------------
*(1) data for "IDEAL" fee adjustments===========================================
*get invoice value and sampling weight for 225 sampled properties
use "${sdir}/Sample_owners.dta", clear
keep ShinaID plot_code full_cost sub_sample_wt invoice lot_area_inv mtaa
rename (ShinaID plot_code) (shina_id plot_id)
label var sub_sample_wt "Inverse sampling weight (i.e. 1/wt=number of obs in pop from group)"
save "temp/owner_sample_full_cost.dta", replace

*get invoice value for properties that were already paid
use "${sdir}/Merged_Invoices_cln.dta", clear
keep if status_reg=="current"
*NB: not all of the remainder were included in the sample frame (also discarded fees_paid_ptl==0)
keep ShinaID full_cost invoice lot_area_inv mtaa
rename (ShinaID ) (shina_id )
gen early_payer=1
gen sub_sample_wt=1
save "temp/early_payers.dta", replace

*merge together owner sample with early payers
use "${sdir}/Owners_cln.dta", clear
keep shina_id plot_id owner_observed s2_q_3
merge 1:1 shina_id plot_id using "temp/owner_sample_full_cost.dta", nogen
gen early_payer=0
append using "temp/early_payers.dta"


*Update weights to account for attrition
gen weight=1/sub_sample_wt
replace weight=weight*(225/146)*(1217/1149.1438) if early_payer==0

*Make a WTP using also the early payers fees
gen WTP=s2_q_3
replace WTP = full_cost if early_payer==1

save "temp/appended_WTP_early_payers.dta", replace


*(2) data for "LEADER" fee adjustments==========================================
use "${sdir}/Leaders_and_Owners_cln.dta", clear
gen WTP=q42_ldr
gen WTP_cntinc=WTP if trt_grp_ldr==0 | trt_grp_ldr==1

collapse (p50) WTP WTP_cntinc, by(shina_id plot_id) //here I use median leader predicitons, could also use mean, or re-weighted individuals
rename WTP WTP_all
save "temp/leader_WTP_pooled.dta", replace

use "${sdir}/Leaders_and_Owners_cln.dta", clear
gen WTP=q42_ldr

collapse (p50) WTP, by(shina_id plot_id trt_grp_ldr) //here I use median leader predicitons, could also use mean, or re-weighted individuals
reshape wide WTP, i(shina_id plot_id) j(trt_grp_ldr)
rename (WTP0 WTP1 WTP2) (WTP_cnt WTP_inc WTP_stk)
merge 1:m shina_id plot_id using "temp/leader_WTP_pooled.dta", nogen

merge 1:m shina_id plot_id using "temp/appended_WTP_early_payers.dta", nogen

if 1==1{
	drop if owner_observed==0
    *ADD leader predictions early payers, but need to assume the leader predictions for the early payers
	foreach trt in all cntinc cnt stk inc{
		*gen resid=WTP-WTP_`trt'
		gen resid=ln(WTP)-ln(WTP_`trt')
		sum resid, d 
		set seed 24061990
		*replace WTP_`trt'=rnormal(WTP-`r(mean)', `r(sd)') if early_payer==1
		replace WTP_`trt'=exp(rnormal(ln(WTP)-`r(mean)', `r(sd)')) if early_payer==1
		drop resid
		}	
	}

save "temp/appended_WTP_early_payers_leaders.dta", replace

*3) send to python to get optimal params----------------------------------------
export delimited using "temp/appended_WTP_early_payers_leaders.csv", replace


*END--------
clear
